require(["common", "jQuery", "coAjax", "queryString", "href", "hash_routie"], function(jSouper, $, coAjax, QueryString, href, hash_routie) {
	var home_imgs = App.get("bus_loginer.config.home_imgs");
	if (!home_imgs) {
		home_imgs = [];
		App.set("bus_loginer.config.home_imgs", home_imgs);
	}

	function _upload_config(cb) {
		coAjax.put(appConfig.bus.set_config_url, {
			data: JSON.stringify(App.get("bus_loginer.config.home_imgs")),
			key: "home_imgs",
			type: "json"
		}, function(result) {
			App.set("bus_loginer", result.result);
			home_imgs = App.get("bus_loginer.config.home_imgs");
			cb && cb();
		});
	}

	function _add_img(img_url, cb) {
		home_imgs.unshift({
			img_url: img_url
		});
		_upload_config(function() {
			alert("success", "图片上传成功");
			cb && cb();
		});
	};
	App.set("$Event.mainset.show_upload_image", function(e) {
		var img_url = App.get("$Cache.mainset.mainset_new_image");
		if (home_imgs.length >= 5) {
			myConfirm("首页轮播器大图最多只能放置5张，是否自动剔除最后一张？", function _yes() {
				_add_img(img_url, function() {
					_edit_img(img_url, 0)
				});
			})
		} else {
			debugger
			_add_img(img_url, function() {
				_edit_img(img_url, 0)
			});
		}
	});
	App.set("$Event.mainset.upload_config", function() {
		_upload_config(function() {
			alert("success", "修改成功")
		});
	});
	App.set("$Event.mainset.remove_home_image", function(e, vm) {
		vm.remove();
		_upload_config(function() {
			alert("success", "图片已删除");
			App.set('isChange', false);
		});
	});
	//编辑图片
	function _edit_img(img_url, vmIndex) {
		App.set("$Cache.imagecrop.src", img_url);
		App.set("$Cache.imagecrop.vmIndex", vmIndex);
		App.set("isChange", true);
	};
	App.set("$Event.mainset.edit_home_image", function(e, vm) {
		_edit_img(vm.get("img_url"), vm.get("$Index"));
	});
	App.set("$Event.mainset.imagecrop.done", function(e, vm) {
		var vmIndex = App.get("$Cache.imagecrop.vmIndex");
		App.set("bus_loginer.config.home_imgs." + vmIndex + ".img_url", App.get("$Cache.imagecrop.output_src"));
		_upload_config(function() {
			alert("success", "图片裁剪完成");
		});
	});
	App.set("$Event.mainset.imagecrop.cancel", function(e, vm) {
		App.set("$Cache.imagecrop.src", "");
		App.set("isChange", false);
	});
});